package com.edoomath.api.servcice.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.edoomath.api.dao.CourseMaterialDao;
import com.edoomath.api.dao.UserCourseMaterialDao;
import com.edoomath.api.dao.UserCourseTaskDao;
import com.edoomath.api.entity.SigninRequest;
import com.edoomath.api.servcice.UserCourseItemService;
import com.edoomath.entity.course.CourseMaterail;
import com.edoomath.entity.course.MaterialType;
import com.edoomath.entity.course.UserCourseTaskMedia;
import com.edoomath.entity.user.WeappNoticeTicket;
import com.edoomath.entity.usercourse.UserCourseItem;
import com.edoomath.entity.usercourse.UserCourseItemExam;
import com.edoomath.entity.usercourse.UserCourseMaterialCategory;
import com.edoomath.entity.usercourse.UserCourseStat;
import com.edoomath.entity.usercourse.UserCourseTask;

/**
 * 
 * @author mohanlan
 *
 */
@Service("userCourseItemService")
public class UserCourseItemServiceImpl extends DataServiceImpl<UserCourseItem> implements UserCourseItemService {
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@Dao
	private UserCourseTaskDao userCourseTaskDao;
	
	@Dao
	private CourseMaterialDao courseMaterialDao;
	
	@Dao
	private UserCourseMaterialDao userCourseMaterialDao;
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.UserCourseItemService#loadTask(java.lang.Long)
	 */
	public UserCourseTask loadTask(Long id) {
		return userCourseTaskDao.load(id);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.UserCourseItemService#listMedias(com.edoomath.entity.usercourse.UserCourseTask)
	 */
	public List<UserCourseTaskMedia> listMedias(UserCourseTask task) {
		return userCourseTaskDao.listUserCourseTaskMedia(task);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.UserCourseItemService#listUserCourseMaterialCategory(java.lang.String)
	 */
	public List<UserCourseMaterialCategory> listUserCourseMaterialCategory(String unionid) {
		return userCourseMaterialDao.listUserCourseMaterialCategory(unionid);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.UserCourseItemService#signin(com.edoomath.entity.usercourse.UserCourseItem, com.edoomath.entity.usercourse.UserCourseTask)
	 */
	public void signin(UserCourseStat userCourseStat, UserCourseItem userCourseItem, UserCourseTask userCourseTask, List<String> images, String voice, String times, Integer voiceSeconds, String formId) {
		
		if(userCourseTask.isExtra()) {
			
			// 1. change userCourseTask to finish
			genericDaoSupport.update(userCourseTask.signin(images, voice, times, voiceSeconds));
			
			userCourseStat.addFinishedTask();
			genericDaoSupport.update(userCourseStat);
			
			// 3. update userCourseItem finished task
		    genericDaoSupport.update(userCourseItem.finishExtraTask());
			
		} else {
			
			// 1. change userCourseTask to finish
			genericDaoSupport.update(userCourseTask.signin(images, voice, times, voiceSeconds));
			
			userCourseStat.addFinishedTask();
			
			// 2. update userCourseStat's finishedTheme
			if(userCourseItem.getTotalTask() - userCourseItem.getFinishedTask() ==  1) {
				userCourseStat.addFinishedTheme();
				genericDaoSupport.update(userCourseStat);
			}
			
			// 3. new user course item exam 
			this.createCourseItemExam(userCourseItem);
		}
		
		if(StringUtils.isNotBlank(formId)) {
			WeappNoticeTicket weappNoticeTicket = new WeappNoticeTicket(formId, userCourseStat.getUserCourse().getUser(), userCourseTask);
			genericDaoSupport.save(weappNoticeTicket.initliaze());
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.UserCourseItemService#signin(com.edoomath.entity.usercourse.UserCourseStat, com.edoomath.entity.usercourse.UserCourseItem, com.edoomath.entity.usercourse.UserCourseTask, com.edoomath.api.entity.SigninRequest)
	 */
	public void signin(UserCourseStat userCourseStat, UserCourseItem userCourseItem, UserCourseTask userCourseTask,
			SigninRequest signinRequest) {
		
		if(userCourseTask.isExtra()) {
			
			// 1. change userCourseTask to finish
			genericDaoSupport.update(userCourseTask.signin(signinRequest.getTimes()));
			
			this.renewSignTaskMedia(userCourseTask, signinRequest);
			
			userCourseStat.addFinishedTask();
			genericDaoSupport.update(userCourseStat);
			
			// 3. update userCourseItem finished task
		    genericDaoSupport.update(userCourseItem.finishExtraTask());
			
		} else {
			
			// 1. change userCourseTask to finish
			genericDaoSupport.update(userCourseTask.signin(signinRequest.getTimes()));
			
			userCourseStat.addFinishedTask();
			
			this.renewSignTaskMedia(userCourseTask, signinRequest);
			
			// 2. update userCourseStat's finishedTheme
			if(userCourseItem.getTotalTask() - userCourseItem.getFinishedTask() ==  1) {
				userCourseStat.addFinishedTheme();
				genericDaoSupport.update(userCourseStat);
			}
			
			// 3. new user course item exam 
			this.createCourseItemExam(userCourseItem);
			
		}
		
		if(StringUtils.isNotBlank(signinRequest.getFormId())) {
			WeappNoticeTicket weappNoticeTicket = new WeappNoticeTicket(signinRequest.getFormId(), userCourseStat.getUserCourse().getUser(), userCourseTask);
			genericDaoSupport.save(weappNoticeTicket.initliaze());
		}
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.UserCourseItemService#reSignin(com.edoomath.entity.usercourse.UserCourseStat, com.edoomath.entity.usercourse.UserCourseItem, com.edoomath.entity.usercourse.UserCourseTask, java.util.List, java.lang.String)
	 */
	public void reSignin(UserCourseStat userCourseStat, UserCourseItem userCourseItem, UserCourseTask userCourseTask,
			List<String> images, String voice, String times, Integer voiceSeconds , String formId) {
		
		genericDaoSupport.update(userCourseTask.signin(images, voice, times, voiceSeconds));
		
		if(StringUtils.isNotBlank(formId)) {
			WeappNoticeTicket weappNoticeTicket = new WeappNoticeTicket(formId, userCourseStat.getUserCourse().getUser(), userCourseTask);
			genericDaoSupport.save(weappNoticeTicket.initliaze());
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.UserCourseItemService#reSignin(com.edoomath.entity.usercourse.UserCourseStat, com.edoomath.entity.usercourse.UserCourseItem, com.edoomath.entity.usercourse.UserCourseTask, com.edoomath.api.entity.SigninRequest)
	 */
	public void reSignin(UserCourseStat userCourseStat, UserCourseItem userCourseItem, UserCourseTask userCourseTask, SigninRequest signinRequest) {
		
		genericDaoSupport.update(userCourseTask.signin(signinRequest.getTimes()));
		
		this.renewSignTaskMedia(userCourseTask, signinRequest);
		
		if(StringUtils.isNotBlank(signinRequest.getFormId())) {
			WeappNoticeTicket weappNoticeTicket = new WeappNoticeTicket(signinRequest.getFormId(), userCourseStat.getUserCourse().getUser(), userCourseTask);
			genericDaoSupport.save(weappNoticeTicket.initliaze());
		}
		
	}
	
	/**
	 * 
	 * @param userCourseTask
	 * @param signinRequest
	 */
	private void renewSignTaskMedia(UserCourseTask userCourseTask, SigninRequest signinRequest) {
		
		List<UserCourseTaskMedia> oldMedias = userCourseTaskDao.listUserCourseTaskMedia(userCourseTask);
		for(UserCourseTaskMedia media: oldMedias) {
			genericDaoSupport.update(media.disable());
		}
		
		if(CollectionUtils.isNotEmpty(signinRequest.getImages())) {
			for(String image : signinRequest.getImages()) {
				UserCourseTaskMedia media = new UserCourseTaskMedia(userCourseTask, image);
				genericDaoSupport.save(media.init());
			}
		}
		
		
		if(CollectionUtils.isNotEmpty(signinRequest.getVoices())) {
			
			for(int i = 0 ; i < signinRequest.getVoices().size(); i++) {
				String voice = signinRequest.getVoices().get(i);
				Integer voiceSecond = signinRequest.getVoiceSeconds().get(i);
				UserCourseTaskMedia media = new UserCourseTaskMedia(userCourseTask, voice, voiceSecond);
				genericDaoSupport.save(media.init());
			}
			
		}
		
	}
	
	/**
	 * 
	 * @param userCourseItem
	 */
	private void createCourseItemExam(UserCourseItem userCourseItem) {
		
		List<String> types = new ArrayList<String>();
		types.add(MaterialType.EXERCISES_EXAM.name());
		List<CourseMaterail> examMaterials = courseMaterialDao.listMaterials(userCourseItem.getCourseItem(), types);
		
		if(CollectionUtils.isEmpty(examMaterials)) {
			
			genericDaoSupport.update(userCourseItem.finishTask());
			
		} else {
			
			genericDaoSupport.update(userCourseItem.examTask());
			
			// create new UserCourseItemExam
			UserCourseItemExam exam = new UserCourseItemExam(userCourseItem.getUnionid(), userCourseItem, examMaterials.get(0));
			genericDaoSupport.save(exam.initliaze());
		}
	}
}
